package com.redrose.rxandroiddemo.main;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityManager;

import androidx.appcompat.app.AppCompatActivity;

import com.redrose.rxandroiddemo.R;
import com.redrose.rxandroiddemo.base.BaseFragment;
import com.redrose.rxandroiddemo.databinding.FragmentHomeBinding;
import com.redrose.rxandroiddemo.gallery.GalleryActivity;
import com.redrose.rxandroiddemo.service.MyService;

import java.util.List;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {
    private String pkgName = "com.xunmeng.merchant";
    private AccessibilityManager mAccessibilityManager;

    @Override
    protected void initView() {
        mBind.openHelper.setOnClickListener((v)->{
            start();
        });
        mBind.openGallery.setOnClickListener((v)->{
           Intent intent = new Intent(getContext(), GalleryActivity.class);
           startActivity(intent);
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    public static final String TAG = "HomeFragment";

    /**
     * 判断是否开启了辅助功能
     *
     * @return
     */
    private boolean isAccessibilitySettingsOn() {
        int accessibilityEnabled = 0;
        // MyService为对应的服务
        final String service = getContext().getPackageName() + "/" + MyService.class.getCanonicalName();
        Log.i(TAG, "service:" + service);
        try {
            accessibilityEnabled = Settings.Secure.getInt(getContext().getContentResolver(),
                    android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
            Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
        } catch (Settings.SettingNotFoundException e) {
            Log.e(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {
            Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
            String settingValue = Settings.Secure.getString(getContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();

                    Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
                    if (accessibilityService.equalsIgnoreCase(service)) {
                        Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
                        return true;
                    }
                }
            }
        } else {
            Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
        }
        return false;
    }

    private void start() {
        if (isAccessibilitySettingsOn()) {
            //某小视频，以xxxx代替包名
            openOtherApp(pkgName);
        } else {
            goAccess();
        }
    }

    /**
     * 前往开启辅助服务界面
     */
    public void goAccess() {
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void openOtherApp(String s) {
        PackageManager packageManager = getContext().getPackageManager();
        Intent i = packageManager.getLaunchIntentForPackage(s);
        startActivity(i);
    }
}